package leetcode.leetcode_1493;

class Solution {
    static public int longestSubarray(int[] nums) {
        int left = 0;
        int right = 0;
        int n = nums.length;
        int ans = 0;
        int count = 0;
        while (right < n) {
            if (nums[right] == 0) {
                count++;
                while(count>1){
                    if(nums[left]==0){
                        count--;
                    }
                    left++;
                }
            }
            ans = Math.max(ans, right - left);
            right++;
        }
        return ans;
    }

    public static void main(String[] args) {
//[0,1,1,1,0,1,1,0,1]
        int[] nums = {0, 1, 1, 1, 0, 1, 1, 0, 1};
        System.out.println(longestSubarray(nums));
    }
}